package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 用户服务
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    //根据用户名查询用户信息,包括用户的角色和角色关联的权限
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);//根据用户名查询用户表
        if(user == null){
            return null;
        }
        Integer userId = user.getId();
        //根据用户id查询关联的角色
        Set<Role> roles = roleDao.findByUserId(userId);
        if(roles != null && roles.size() > 0){
            //遍历角色集合，查询每个角色关联的权限
            for (Role role : roles) {
                Integer roleId = role.getId();//角色id
                //根据角色id查询关联的权限
                Set<Permission> permissions = permissionDao.findByRoleId(roleId);
                if(permissions != null && permissions.size() > 0){
                    //角色关联权限集合
                    role.setPermissions(permissions);
                }
            }
            //用户关联角色集合
            user.setRoles(roles);
        }

        return user;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //当前页码
        Integer currentPage = queryPageBean.getCurrentPage();
        //一页多少条
        Integer pageSize = queryPageBean.getPageSize();
        //查询条件
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        //判断查询条件是否为空
        Page<User> userPage=null;
        if (queryString!=null&&queryString.length()>0){
            queryString="%"+queryString+"%";
            userPage=userDao.findPage(queryString);
        }else {
            userPage=userDao.findPage(queryString);
        }
        long total = userPage.getTotal();
        List<User> result = userPage.getResult();
        for (User user : result) {
            Date birthday = user.getBirthday();
            try {
              String b=birthday+"";
                birthday = DateUtils.parseString2Date(b);
                user.setBirthday(birthday);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        PageResult pageResult=new PageResult(total,result);

        return pageResult;
    }

    @Override
    public User findById(Integer id) {
        User user=userDao.findById(id);
        return user;
        
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public List<Integer> findByRoleId(Integer roleId) {
        return roleDao.findByRoleId(roleId);
    }

    @Override
    public void edit(User user, Integer[] checkitemIds) {
        //先修改用户信息
        userDao.edit(user);
        //删除中间表的关联项
        userDao.deleteByUserId(user.getId());
        //删除关联的角色
        if (checkitemIds!=null&&checkitemIds.length>0){
            for (Integer id : checkitemIds) {
                //重新建立关系
                Integer userId = user.getId();
                userDao.addId(userId,id);
            }
        }
    }

    @Override
    public void add(User user, Integer[] checkitemIds) {
        userDao.add(user);
        Integer userId = user.getId();
        if (checkitemIds!=null&&checkitemIds.length>0){
            for (Integer id : checkitemIds) {
                userDao.addMidId(userId,id);
            }

        }

    }

    @Override
    public void delete(Integer id) {
        userDao.deleteByUserId(id);
        userDao.delete(id);
    }
}
